package com.xhwl.logistics.service.business;

import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.dao.mapper.IVehicleParkingLotInfoMapper;
import com.xhwl.logistics.dto.http.ParkingLotAreaInfoKeTopDTO;
import com.xhwl.logistics.dto.http.ParkingLotAreaKeTopDTO;
import com.xhwl.logistics.dto.http.ParkingLotPassageInfoKeTopDTO;
import com.xhwl.logistics.dto.http.ParkingLotPassageKeTopDTO;
import com.xhwl.logistics.dto.http.query.QueryCommonKeTopDTO;
import com.xhwl.logistics.dto.http.query.QueryParkingLotPassageKeTopDTO;
import com.xhwl.logistics.entity.VehicleArea;
import com.xhwl.logistics.entity.VehiclePassage;
import com.xhwl.logistics.po.VehicleAreaPO;
import com.xhwl.logistics.po.VehicleParkingLotInfoPO;
import com.xhwl.logistics.po.VehicleParkingLotPO;
import com.xhwl.logistics.po.VehiclePassageSecondPO;
import com.xhwl.logistics.service.impl.VehicleAreaServiceImpl;
import com.xhwl.logistics.service.impl.VehiclePassageServiceImpl;
import com.xhwl.logistics.service.impl.http.KeTopHttpServiceImpl;
import com.xhwl.logistics.service.impl.redis.VehicleIntegratedConfigRedisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

//@Service
public class VehicleAdminBusinessService {

    @Autowired
    private VehicleIntegratedConfigRedisServiceImpl vehicleIntegratedConfigRedisService;

    @Autowired
    private IVehicleParkingLotInfoMapper vehicleParkingLotInfoMapper;

    @Autowired
    private VehicleAreaServiceImpl vehicleAreaService;

    @Autowired
    private VehiclePassageServiceImpl vehiclePassageService;

    @Autowired
    private KeTopHttpServiceImpl keTopHttpService;

    @Transactional(rollbackFor = {Exception.class})
    public void adminSyncKeTopParkingLotInfo(Integer projectId) {
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = vehicleIntegratedConfigRedisService.queryIntegratedConfigByProjectId(projectId);

        Optional<VehicleParkingLotInfoPO> parkingLotInfoPOOptional = vehicleParkingLotInfoMapper.queryParkingLotInfoByProjectId(projectId);
        VehicleParkingLotInfoPO vehicleParkingLotInfoPO = parkingLotInfoPOOptional.orElseThrow(() -> new BusinessException("该项目不存在车场的基本信息"));

        List<VehicleParkingLotPO> vehicleParkingLotPOList = vehicleParkingLotInfoPO.getVehicleParkingLotPOList();
        if (vehicleParkingLotPOList.size()!=1) {
            throw new BusinessException("该业务只支持科拓厂商下，一个项目只有一个车场的情况");
        }
        VehicleParkingLotPO vehicleParkingLotPO = vehicleParkingLotPOList.get(0);
        String parkId = vehicleParkingLotPO.getParkId();
        Assert.hasLength(parkId, "车场的parkId不能为空和null");

        List<VehicleAreaPO> vehicleAreaPOList = vehicleParkingLotPO.getVehicleAreaPOList();
        Map<String, VehicleAreaPO> mapForArea = vehicleAreaPOList.stream().collect(Collectors.toMap(VehicleAreaPO::getName, Function.identity()));
        ParkingLotAreaKeTopDTO parkingLotAreaKeTopDTO = keTopHttpService.queryParkingLotArea(vehicleIntegratedConfigBO, new QueryCommonKeTopDTO());
        List<ParkingLotAreaInfoKeTopDTO> areaInfoKeTop = parkingLotAreaKeTopDTO.getAreaInfo();
        for (ParkingLotAreaInfoKeTopDTO parkingLotAreaInfoKeTopDTO : areaInfoKeTop) {
            String areaName = parkingLotAreaInfoKeTopDTO.getAreaName();
            VehicleAreaPO vehicleAreaPO = mapForArea.get(areaName);
            if (Objects.nonNull(vehicleAreaPO)) {
                VehicleArea vehicleArea = new VehicleArea();
                vehicleArea.setId(vehicleAreaPO.getId());
                vehicleArea.setThirdAreaId(parkId + "-" + parkingLotAreaInfoKeTopDTO.getAreaCode());
                vehicleAreaService.updateById(vehicleArea);
            }
        }

        ParkingLotPassageKeTopDTO parkingLotPassageKeTopDTO = keTopHttpService.queryParkingLotPassage(vehicleIntegratedConfigBO, new QueryParkingLotPassageKeTopDTO());
        Integer pageSize = parkingLotPassageKeTopDTO.getPageSize();
        Integer totalCount = parkingLotPassageKeTopDTO.getTotalCount();
        if (null!=pageSize && null!=totalCount && totalCount>pageSize) {
            throw new BusinessException("同步车场通道信息时，通道总数大于目前的分页数（pageSize）20，因此通道信息更新thirdPassageId会不完整");
        }
        Map<String, Integer> mapForAreaCode = areaInfoKeTop.stream().collect(Collectors.toMap(ParkingLotAreaInfoKeTopDTO::getAreaName, ParkingLotAreaInfoKeTopDTO::getAreaCode));
        for (VehicleAreaPO vehicleAreaPO : vehicleAreaPOList) {
            List<VehiclePassageSecondPO> vehiclePassageSecondPOList = vehicleAreaPO.getVehiclePassageSecondPOList();
            Map<String, VehiclePassageSecondPO> mapForPassage = vehiclePassageSecondPOList.stream().collect(Collectors.toMap(VehiclePassageSecondPO::getName, Function.identity()));
            for (ParkingLotPassageInfoKeTopDTO parkingLotPassageInfoKeTopDTO : parkingLotPassageKeTopDTO.getNodeList()) {
                VehiclePassageSecondPO vehiclePassageSecondPO = mapForPassage.get(parkingLotPassageInfoKeTopDTO.getNodeName());
                if (Objects.nonNull(vehiclePassageSecondPO)) {
                    VehiclePassage vehiclePassage = new VehiclePassage();
                    vehiclePassage.setId(vehiclePassageSecondPO.getId());

                    Integer areaCode = mapForAreaCode.get(vehicleAreaPO.getName());
                    if (Objects.isNull(areaCode)) {
                        throw new BusinessException("已同步的车场通道信息所属的区域，从科拓厂商的区域信息找不到相应的areaCode（不满足适用范围，厂商科拓车场基本信息中区域名称和id已经变化了）");
                    }
                    vehiclePassage.setThirdPassageId(parkId + "-" + areaCode + "-" + parkingLotPassageInfoKeTopDTO.getId());
                    vehiclePassageService.updateById(vehiclePassage);
                }
            }
        }
    }

}
